package cn.wsalix.observable.service.impl;

import java.util.Observable;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.admin.BaseUser;
import cn.wsalix.observable.service.AddressChangeService;
import cn.wsalix.user.entity.Address;
import cn.wsalix.user.form.AddressForm;
import cn.wsalix.user.service.AddressService;
import cn.wsalix.user.service.UserInfoService;
import cn.wsalix.utils.UserUtils;

@Service("addressChangeService")
public class AddressChangeServiceImpl extends Observable implements
		AddressChangeService {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private AddressService addressService;
	private Address address;

	@PostConstruct
	public void init() {
		logger.info("==========init");
		addObserver(userInfoService);
		addObserver(addressService);
	}

	@PreDestroy
	public void destroy() {
		logger.info("==========destroy");
	}

	@Transactional
	@Override
	public Address change(AddressForm entity) {
		setChanged();
		notifyObservers(entity);
		return address;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@Autowired
	protected UserUtils userUtils;

	@Transactional
	@Override
	public Address createAddress() {
		BaseUser user = userUtils.getInUser();
		AddressForm form = new AddressForm();
		form.setUserId(user.getId());
		return change(form);
	}

}
